package com.son.hotel.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.son.hotel.domain.RoomType;
import com.son.hotel.domain.Rooms;
import com.son.hotel.response.RoomDetailInfoResp;
import com.son.hotel.service.RoomTypeService;
import com.son.hotel.mapper.RoomTypeMapper;
import com.son.hotel.service.RoomsService;
import com.son.hotel.vo.CustomAndRoomInfo;
import com.son.hotel.vo.SearchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 孙一卓
* @description 针对表【room_type】的数据库操作Service实现
* @createDate 2025-06-24 15:03:59
*/
@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType>
    implements RoomTypeService{

    @Autowired
    private RoomsService roomsService;

    @Override
    public List<RoomDetailInfoResp> listAllRoomInfo(List<Rooms> roomList, List<RoomType> roomTypeList) {
        ArrayList<RoomDetailInfoResp> list = new ArrayList<>();
        //将roomType装进Map中，并且以id为key
        Map<Integer, RoomType> roomTypeMap = roomTypeList.stream()
                .collect(Collectors.toMap(RoomType::getId, r -> r));
        for (Rooms rooms : roomList) {
            Integer typeId = rooms.getTypeId();
            Integer roomId = rooms.getId();
            Integer status = rooms.getStatus();
            //根据typeId从Map中获取roomType信息
            RoomType roomType = roomTypeMap.get(typeId);
            String name = roomType.getName();
            Integer price = roomType.getPrice();
            //装到List中
            RoomDetailInfoResp roomDetailInfoResp = new RoomDetailInfoResp();
            roomDetailInfoResp.setRoomId(roomId);
            roomDetailInfoResp.setStatus(status);
            roomDetailInfoResp.setPrice(price);
            roomDetailInfoResp.setRoomName(name);
            list.add(roomDetailInfoResp);
        }
        return list;
    }

    /**
     * 让roomType中的房间数量减一
     * @param typeId
     */
    @Override
    public void updateRoomTypeInfoDec(int typeId) {
        UpdateWrapper<RoomType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("quantity = quantity - 1").eq("id",typeId);
        this.update(updateWrapper);
    }

    /**
     * 让roomType中的房间数量加一
     * @param typeId
     */
    @Override
    public void updateRoomTypeInfoInc(int typeId) {
        UpdateWrapper<RoomType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("quantity = quantity + 1").eq("id",typeId);
        this.update(updateWrapper);
    }

    @Override
    public List<RoomDetailInfoResp> searchRoom(SearchInfo searchInfo) {
        List<Rooms> roomList = roomsService.list();
        List<RoomType> roomTypeList = this.list();
        //拿到了房间的展示信息
        List<RoomDetailInfoResp> roomDetailInfoResp = listAllRoomInfo(roomList, roomTypeList);

        String key = searchInfo.getKey();
        String value = searchInfo.getValue();
        //房间的类型
        if ("类型".equals(key)){
            return roomDetailInfoResp.stream().filter(info -> info.getRoomName().contains(value)).toList();
        }
        //房间的状态
        else if ("状态".equals(key)){
            //查询状态 将String类型转成int类型
            int status = statusStringToInt(value);
            return roomDetailInfoResp.stream().filter(info->info.getStatus() == status).toList();
        }
        //房间的房号
        else return roomDetailInfoResp.stream()
                    .filter(info-> Convert.toStr(info.getRoomId()).contains(value))
                    .toList();
    }

    /**
     * 状态字段从String转成int
     * @param value
     * @return
     */
    public int statusStringToInt(String value){
        if ("空闲".equals(value))
            return 0;
        else if ("已住".equals(value))
            return 1;
        else
            return 2;
    }

    /**
     * roomName从String转换成int
     * @param name
     * @return
     */
    @Override
    public int roomNameStringToInt(String name){
        if (name.equals("大床房")){
            return 1;
        }
        else if (name.equals("双床房")){
            return 2;
        }
        else return 3;
    }

}




